package com.itliang.web.controller.file;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itliang.core.model.dto.equipment.EquipmentDataQueryRequest;
import com.itliang.core.model.dto.system.sysuser.SysUserQueryRequest;
import com.itliang.core.model.entity.system.SysUser;
import com.itliang.core.model.vo.SysUserVO;
import com.itliang.core.result.BaseResponse;
import com.itliang.core.result.ResultUtils;
import com.itliang.exception.BusinessException;
import com.itliang.service.hardware.EquipmentDataService;
import com.itliang.service.system.SysUserService;
import com.itliang.utils.easyexcel.EasyExcelUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author : liangzhuoyuan
 */
@Api(tags = "文件管理接口")
@RestController
@RequestMapping("/admin/file")
public class FileController {

    @Resource
    private EquipmentDataService equipmentDataService;

    @Resource
    private SysUserService sysUserService;

    @GetMapping("/equipment/data/excel")
    public void exportEquipmentDataExcel(EquipmentDataQueryRequest queryRequest, HttpServletResponse response) {
        equipmentDataService.exportEquipmentDataExcelByDataType(queryRequest, response);
    }

    @GetMapping("/user/excel")
    public void exportUserExcel(SysUserQueryRequest sysUserQueryRequest, HttpServletResponse response) {
        SysUser sysUser = new SysUser();
        if (sysUserQueryRequest != null) {
            BeanUtils.copyProperties(sysUserQueryRequest, sysUser);
        }
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>(sysUser);
        List<SysUser> userList = sysUserService.list(queryWrapper);
        List<SysUserVO> userVOList = userList.stream().map(user -> {
            SysUserVO userVO = new SysUserVO();
            BeanUtils.copyProperties(user, userVO);
            return userVO;
        }).collect(Collectors.toList());
        EasyExcelUtils.writeExcel(userVOList, SysUserVO.class, "用户信息", response);
    }

    @PostMapping("/user/upload")
    public BaseResponse<?> exportUserExcel(MultipartFile file) throws IOException {

        if (file == null) {
            throw new BusinessException(566558, "上传头像失败");
        }
        String path = "D:\\test_touxiang_upload";
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
        File newFile = new File(path + "/" + newFileName);
        file.transferTo(newFile);
        return ResultUtils.success(newFileName);
    }

}
